package com.bbva.kyfb.kyfbjpatlbh.dominiotlbh;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

/**
 * The Class TtlsbdivDataOnDemand.
 */
@Configurable
@Component
@RooDataOnDemand(entity = Ttlsbdiv.class)
public class TtlsbdivDataOnDemand {

	/** The rnd. */
	private Random rnd = new SecureRandom();

	/** The data. */
	private List<Ttlsbdiv> data;

	/**
	 * Gets the new transient ttlsbdiv.
	 *
	 * @param index the index
	 * @return the new transient ttlsbdiv
	 */
	public Ttlsbdiv getNewTransientTtlsbdiv(int index) {
        Ttlsbdiv obj = new Ttlsbdiv();
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setCodAbreviat(obj, index);
        setCodUnidcamb(obj, index);
        setDesDiisoalf(obj, index);
        setQtyEuro(obj, index);
        setXsnDecimal(obj, index);
        setXsnMoneuro(obj, index);
        setCodDiisoalf(obj, index);
        return obj;
    }

	/**
	 * Sets the aud fmodific.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudFmodific(Ttlsbdiv obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	/**
	 * Sets the aud usuario.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudUsuario(Ttlsbdiv obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	/**
	 * Sets the cod diisoalf.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodDiisoalf(Ttlsbdiv obj, int index) {
        String diisoa = String.valueOf(index);
        if (diisoa.length() > 3) {
        	diisoa = diisoa.substring(0, 3);
        }
        obj.setCodDiisoalf(diisoa);
    }

	
	/**
	 * Sets the cod abreviat.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodAbreviat(Ttlsbdiv obj, int index) {
        String codAbreviat = "c_" + index;
        if (codAbreviat.length() > 3) {
            codAbreviat = codAbreviat.substring(0, 3);
        }
        obj.setCodAbreviat(codAbreviat);
    }

	/**
	 * Sets the cod unidcamb.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodUnidcamb(Ttlsbdiv obj, int index) {
		BigDecimal codUnidcamb = BigDecimal.valueOf(index);
		if (String.valueOf(index).length() > 3){
			obj.setCodUnidcamb(BigDecimal.valueOf(Integer.valueOf(String.valueOf(index).substring(0,3))));
		}else{
			obj.setCodUnidcamb(codUnidcamb);
		}
        
    }

	/**
	 * Sets the des diisoalf.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesDiisoalf(Ttlsbdiv obj, int index) {
        String desDiisoalf = "desDiisoalf_" + index;
        if (desDiisoalf.length() > 40) {
            desDiisoalf = desDiisoalf.substring(0, 40);
        }
        obj.setDesDiisoalf(desDiisoalf);
    }

	/**
	 * Sets the qty euro.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setQtyEuro(Ttlsbdiv obj, int index) {
        BigDecimal qtyEuro = BigDecimal.valueOf(index);
        if (qtyEuro.compareTo(new BigDecimal("999999.999999")) == 1) {
            qtyEuro = new BigDecimal("999999.999999");
        }
        obj.setQtyEuro(qtyEuro);
    }

	/**
	 * Sets the xsn decimal.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXsnDecimal(Ttlsbdiv obj, int index) {
        Character xsnDecimal = new Character('N');
        obj.setXsnDecimal(xsnDecimal);
    }

	/**
	 * Sets the xsn moneuro.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXsnMoneuro(Ttlsbdiv obj, int index) {
        Character xsnMoneuro = new Character('N');
        obj.setXsnMoneuro(xsnMoneuro);
    }

	/**
	 * Gets the specific ttlsbdiv.
	 *
	 * @param index the index
	 * @return the specific ttlsbdiv
	 */
	public Ttlsbdiv getSpecificTtlsbdiv(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Ttlsbdiv obj = data.get(index);
        return Ttlsbdiv.findTtlsbdiv(obj.getCodDiisoalf());
    }

	/**
	 * Gets the random ttlsbdiv.
	 *
	 * @return the random ttlsbdiv
	 */
	public Ttlsbdiv getRandomTtlsbdiv() {
        init();
        Ttlsbdiv obj = data.get(rnd.nextInt(data.size()));
        return Ttlsbdiv.findTtlsbdiv(obj.getCodDiisoalf());
    }

	/**
	 * Modify ttlsbdiv.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTtlsbdiv(Ttlsbdiv obj) {
        return false;
    }

	/**
	 * Inits the.
	 */
	public void init() {
        data = Ttlsbdiv.findTtlsbdivEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Ttlsbdiv' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlbh.dominiotlbh.Ttlsbdiv>();
        for (int i = 0; i < 10; i++) {
            Ttlsbdiv obj = getNewTransientTtlsbdiv(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
